package com.aosgame;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.Rect;

import com.androidgame.fx.Graphics;
import com.androidgame.fx.ScreenConfig;

public class Minion extends Entity {

	public Entity	target;
	private int		scrWidth;
	private int		scrHeight;
	private long	attackDelayTime;
	
	public Minion(Bitmap image) {
		this.image = image;
		position = new Point();
		collisionArea = new Rect();
		maxHpRect = new Rect();
		hpRect = new Rect();
		angle = 0;
		hp = maxHp = 100;
		power = 10;
		armor = 3;
		speed = 1;
		remainX = 0;
		remainY = 0;
		state = STATE_WALK;
		
		frameWidth = image.getWidth() / 32;
		frameHeight = image.getHeight() / 8;
		
		scrWidth = ScreenConfig.virtualScreenWidth;
		scrHeight = ScreenConfig.virtualScreenHeight;
		
		animations = new Animation[ANI_END];
		animations[ANI_IDLE] = new Animation(true, 0, 4, 200);
		animations[ANI_WALK] = new Animation(true, 4, 8, 150);
		animations[ANI_ATTACK] = new Animation(false, 12, 4, 100);
		animations[ANI_DEAD] = new Animation(false, 20, 4, 300);
		currentAnimation = animations[ANI_IDLE];
	}
	
	public void init() {
		alive = true;
		int ranX = (int)(Math.random() * Map.width);
		int ranY = (int)(Math.random() * Map.height);
		setPosition(ranX, ranY);
	}
	
	public void update() {
		if(compareState(STATE_DEAD)) {
			if(currentAnimation.play == false) {
				alive = false;
			}
		} else {
			aiLogic();
		}
		
		animation();
	}
	
	public void present(Graphics g, Player focus) {
		int scrX, scrY;
		
		if(focus.position.x < scrWidth / 2) {
			scrX = position.x % scrWidth;
		} else if(focus.position.x > Map.width - scrWidth / 2) {
			scrX = position.x % scrWidth;
		} else {
			scrX = scrWidth / 2 + position.x - focus.position.x;
		}
		
		if(focus.position.y < scrHeight / 2) {
			scrY = position.y % scrHeight;
		} else if(focus.position.y > Map.height - scrHeight / 2) {
			scrY = position.y % scrHeight;
		} else {
			scrY = scrHeight / 2 + position.y - focus.position.y;
		}

		maxHpRect.set(scrX - 32, scrY - frameHeight / 3, scrX + 32, scrY - frameHeight / 3 + 10);
		hpRect.set(scrX - 32, scrY - frameHeight / 3, scrX - 32 + 64 * hp / maxHp, scrY - frameHeight / 3 + 10);
		
		if(!compareState(STATE_DEAD)) {
			g.drawRect(maxHpRect, Color.WHITE);
			g.drawRect(hpRect, Color.RED);
		}
		g.drawBitmap(image, scrX - frameWidth / 2, scrY - frameHeight / 2, 
				(currentAnimation.startFrame + frame) * frameWidth, drt * frameHeight, frameWidth, frameHeight);
	}
	
	public void movement() {
		remainX += (float)Math.cos(angle) * speed;
		remainY += (float)-Math.sin(angle) * speed;
		
		move((int)remainX, (int)remainY);
				
		remainX -= (int)remainX;
		remainY -= (int)remainY;
		
		int x = 0;
		int y = 0;
		boolean mapOut = false;
		if(collisionArea.left < 0) {
			x = collisionArea.width() / 2;
			mapOut = true;
		} else if(collisionArea.right > Map.width) {
			x = Map.width - collisionArea.width() / 2;
			mapOut = true;
		} else {
			x = position.x;
		}
		if(collisionArea.top < 0) {
			y = collisionArea.height() / 2;
			mapOut = true;
		} else if(collisionArea.bottom > Map.height){
			y = Map.height - collisionArea.height() / 2;
			mapOut = true;
		} else {
			y = position.y;
		}
		
		if(mapOut == true) setPosition(x, y);
	}
	
	public void setTarget(Entity target) {
		this.target = target;
	}
	
	public void aiLogic() {
		long currentTime = System.currentTimeMillis();
		angle = getAngle(position.x, position.y, target.position.x, target.position.y);
		processDrt();
		
		if(target.compareState(STATE_DEAD)) { 
			setAnimation(ANI_IDLE);
			return;
		}
		
		if(getDistance(position.x, position.y, target.position.x, target.position.y) > 80) {
			removeState(STATE_ATTACK);
			addState(STATE_WALK);
		} else {
			removeState(STATE_WALK);
			addState(STATE_ATTACK);
		}
		
		
		if(compareState(STATE_WALK)) {
			setAnimation(ANI_WALK);
			movement();
		} 
		
		if(compareState(STATE_ATTACK)) {
			if(target.compareState(STATE_DEAD) == false) {
				if (attackDelayTime < currentTime) {
					attackDelayTime = currentTime + 2000;
					setAnimation(ANI_ATTACK);
					target.damaged(power);
				}
			}

			if(currentAnimation.play == false) {
				setAnimation(ANI_IDLE);
			}	
		}
	}
}
